﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace OneMile.Common
{
    /// <summary>
    /// 错误日志
    /// </summary>
    public static class ErrorLogWriter
    {
        private static object LockObject = new object();

        /// <summary>
        /// 错误日志路径
        /// </summary>
        public static string BaseDirectory
        {
            get;
            set;
        }

        /// <summary>
        /// 错误文件名
        /// </summary>
        private static string FileName
        {
            get
            {
                string basePath = BaseDirectory;
                if (string.IsNullOrEmpty(basePath))
                {
                    basePath = AppDomain.CurrentDomain.BaseDirectory;
                }
                string errorFilePath = Path.Combine(basePath, "ErrorLogs");
                if (!Directory.Exists(errorFilePath))
                {
                    Directory.CreateDirectory(errorFilePath);
                }
                return Path.Combine(errorFilePath, DateTime.Today.ToString("yyyy-MM-dd") + ".txt");
            }
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="ex">异常详情</param>
        public static void Write(Exception ex)
        {
            if (ex == null)
            {
                return;
            }

            try
            {
                lock (LockObject)
                {
                    using (StreamWriter sw = File.AppendText(FileName))
                    {
                        sw.WriteLine("----------------------------------------------------------");
                        sw.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        sw.WriteLine("异常：");
                        sw.WriteLine(ex.ToString());
                        sw.WriteLine("----------------------------------------------------------");
                        sw.WriteLine();
                        sw.Close();
                    }
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="error">原因</param>
        public static void Write(string error)
        {
            Write(new Exception(error));
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="error">原因</param>
        /// <param name="ex">错误详情</param>
        public static void Write(string error, Exception ex)
        {
            if (ex == null)
            {
                return;
            }

            try
            {
                lock (LockObject)
                {
                    using (StreamWriter sw = File.AppendText(FileName))
                    {
                        sw.WriteLine("----------------------------------------------------------");
                        sw.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        sw.WriteLine("原因：" + error);
                        sw.WriteLine("异常：");
                        sw.WriteLine(ex.ToString());
                        sw.WriteLine("----------------------------------------------------------");
                        sw.WriteLine();
                        sw.Close();
                    }
                }
            }
            catch
            {

            }
        }
    }
}
